package net.kaoslabs;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import net.kaoslabs.extras.TextUtil;

public class PlayerListener implements Listener {
	public static CustomJoinMessage plugin;
	public static String message;
	public static String user;
	
	public PlayerListener(CustomJoinMessage instance) {
		plugin = instance;
	}

	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent event) {
		if (plugin.getConfig().getBoolean("use-default") == true) {
			message = TextUtil.colorizeText(plugin.getConfig().getString("default.join-message").replace("%u", event.getPlayer().getName()));
			if (plugin.getConfig().getBoolean("use-groups")) {
				if (plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".join-message") != null) {
					message = TextUtil.colorizeText(plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".join-message").replace("%u",event.getPlayer().getName()));
				}
			}
			if (plugin.getConfig().getString("users."+event.getPlayer().getName()+".join-message") != null) {
				message = TextUtil.colorizeText(plugin.getConfig().getString("users."+event.getPlayer().getName()+".join-message").replace("%u", event.getPlayer().getName()));
			}
			if (message != null) {
				event.setJoinMessage(ChatColor.YELLOW + message);
			}
		} else {
			if (plugin.getConfig().getBoolean("use-groups")) {
				if (plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".join-message") != null) {
					message = TextUtil.colorizeText(plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".join-message").replace("%u",event.getPlayer().getName()));
				}
			}
			if (plugin.getConfig().getString("users."+event.getPlayer().getName()+".join-message") != null) {
				message = TextUtil.colorizeText(plugin.getConfig().getString("users."+event.getPlayer().getName()+".join-message").replace("%u", event.getPlayer().getName()));
			}
			if (message != null) {
				event.setJoinMessage(ChatColor.YELLOW + message);
			}
		}
	}
	@EventHandler
	public void onPlayerQuit(PlayerQuitEvent event) {
		if (plugin.getConfig().getBoolean("use-default") == true) {
			message = TextUtil.colorizeText(plugin.getConfig().getString("default.leave-message").replace("%u", event.getPlayer().getName()));
			if (plugin.getConfig().getBoolean("use-groups")) {
				if (plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".leave-message") != null) {
					message = TextUtil.colorizeText(plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".leave-message").replace("%u",event.getPlayer().getName()));
				}
			}
			if (plugin.getConfig().getString("users."+event.getPlayer().getName()+".leave-message") != null) {
				message = TextUtil.colorizeText(plugin.getConfig().getString("users."+event.getPlayer().getName()+".leave-message").replace("%u", event.getPlayer().getName()));
			}
			if (message != null) {
				event.setQuitMessage(ChatColor.YELLOW + message);
			}
		} else {
			if (plugin.getConfig().getBoolean("use-groups")) {
				if (plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".leave-message") != null) {
					message = TextUtil.colorizeText(plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".leave-message").replace("%u",event.getPlayer().getName()));
				}
			}
			if (plugin.getConfig().getString("users."+event.getPlayer().getName()+".leave-message") != null) {
				message = TextUtil.colorizeText(plugin.getConfig().getString("users."+event.getPlayer().getName()+".leave-message").replace("%u", event.getPlayer().getName()));
			}
			if (message != null) {
				event.setQuitMessage(ChatColor.YELLOW + message);
			}
		}
	}
	@EventHandler
	public void onPlayerKick(PlayerKickEvent event) {
		if (plugin.getConfig().getBoolean("use-default") == true) {
			message = TextUtil.colorizeText(plugin.getConfig().getString("default.kick-message").replace("%u", event.getPlayer().getName()));
			if (plugin.getConfig().getBoolean("use-groups")) {
				if (plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".kick-message") != null) {
					message = TextUtil.colorizeText(plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".kick-message").replace("%u",event.getPlayer().getName()));
				}
			}
			if (plugin.getConfig().getString("users."+event.getPlayer().getName()+".kick-message") != null) {
				message = TextUtil.colorizeText(plugin.getConfig().getString("users."+event.getPlayer().getName()+".kick-message").replace("%u", event.getPlayer().getName()));
			}
			if (message != null) {
				event.setLeaveMessage(ChatColor.YELLOW + message);
			}
		} else {
			if (plugin.getConfig().getBoolean("use-groups")) {
				if (plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".kick-message") != null) {
					message = TextUtil.colorizeText(plugin.getConfig().getString("groups."+plugin.permission.getPrimaryGroup(event.getPlayer())+".kick-message").replace("%u",event.getPlayer().getName()));
				}
			}
			if (plugin.getConfig().getString("users."+event.getPlayer().getName()+".kick-message") != null) {
				message = TextUtil.colorizeText(plugin.getConfig().getString("users."+event.getPlayer().getName()+".kick-message").replace("%u", event.getPlayer().getName()));
			}
			if (message != null) {
				event.setLeaveMessage(ChatColor.YELLOW + message);
			}
		}
	}
}